<?php 
	require_once  __DIR__."/../silex/vendor/autoload.php";
	require_once  __DIR__."/Helpers/session.php";
	require_once __DIR__.'/Models/series.php';

	use Helpers\Session;

	use Symfony\Component\HttpFoundation\Request;
	use Symfony\Component\HttpFoundation\Response;


	$app = new Silex\Application();
	$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
		'db.options' =>  array(
		        'driver'    => 'pdo_mysql',
		        'host'      => 'mysql.sitescopy.ru',
		        'dbname'    => 'u168219490_media',
		        'user'      => 'u168219490_media',
		        'password'  => 'daKota',
		        'charset'   => 'utf8',
		    ),
		));
	$app->register(new Silex\Provider\TwigServiceProvider(), array(
	    'twig.path' => __DIR__.'/../views',
	));



	$series = new Models\Series($app);	

	$app['controllers']
		->before(function() use ($app) {
				$sid = $_COOKIE['PHPSESSID'];
				
					
				if ( !isset($sid) ) {
					echo "not session <br>";
					session_start();	
					$sid = session_id();
					$_SESSION['uid'] = 0;
					$_SESSION['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
					$sql1 = "SELECT count(*) count FROM session WHERE session_id = ?";
					$res = $app['db']->fetchAssoc($sql1, array( (string) $sid ) );
					if($res['count'] == 0){
						$sql2 = "INSERT INTO session (session_id, session_time ) VALUES (? , now() )";
						$app['db']->executeQuery($sql2, array( (string) $sid ));

					}
				}else{
						session_start();	
						echo "uid 0 ses {$_SESSION['uid']} <br>";

						if( isset($_SESSION['REMOTE_ADDR']) ){
							echo "1 ip isset <br>";
							if($_SESSION['REMOTE_ADDR'] != $_SERVER['REMOTE_ADDR']){
								echo "2 ip invalid <br>";
								Helpers\Session\session_reset($app);
							}else{
								echo "3 ip valid <br>";
								$sid = session_id();
								echo "uid 3 ses {$_SESSION['uid']} <br>";
								$sql1 = "SELECT count(*) count FROM session WHERE session_id = ?";
								$res = $app['db']->fetchAssoc($sql1, array( (string) $sid ) );
								if($res['count'] == 0){
									$sql2 = "UPDATE session SET  session_time = now() WHERE session_id = ?";
									echo "3 sid $sid <br>";
									echo "3 $sql2 <br>";
									$app['db']->executeQuery($sql2, array( (string) $sid ));
								}																	
							}						
						}else{
								session_destroy();
								session_start();
								$sid = session_id();
								$_SESSION['uid'] = 0;
								$_SESSION['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
								$sql1 = "SELECT count(*) count FROM session WHERE session_id = ?";
								$res = $app['db']->fetchAssoc($sql1, array( (string) $sid ) );
								if($res['count'] == 0){
									$sql2 = "INSERT INTO session (session_id, session_time ) VALUES (? , now() )";
									$app['db']->executeQuery($sql2, array( (string) $sid ));
								}									
						}

						$sql3 = "SELECT (TIME_TO_SEC(TIMEDIFF(now(), session_time))/60) livetime  FROM `session` WHERE 1";
						$time = $app['db']->fetchAssoc($sql3)['livetime'];
						echo "livetime $time <br>";						
						if($time > 60 ){
							echo "uid time ses {$_SESSION['uid']} <br>";
							Helpers\Session\session_reset($app);
							$sid = session_id();
							echo "uid ses after reset $sid <br>";

						}
				}

				echo "sid $sid <br>";
				echo "uid $uid <br>";
				echo "uid fin ses {$_SESSION['uid']} <br>";
				echo "ip {$_SESSION['REMOTE_ADDR']} <br>";

			})
			->before(function() use($app) {
				if( !isset($_SESSION['uid']) ){
					$_SESSION['uid']==0;
				}
				$uid = $_SESSION['uid'];
				$sql1 = "SELECT user_login login, usersetting_role role FROM user, user_setting WHERE iduser = ? AND iduser = user_id";
				$res = $app['db']->fetchAssoc($sql1, array( (int)$uid ));
				$_SESSION['login'] = $res['login'];
				$_SESSION['role'] = $res['role'];
			});

	
	$app->get('/', function() use ($app, $series) {
		return $app['twig']->render('index.html', array( 'data_series' => $series ) );
	})
		->before(function() use ($app){
			$uid = $_SESSION['uid'];
			$role = $_SESSION['role'];
			if($uid == 0){
				return $app->redirect('/medialib/login');
			}
			echo 'uid $uid <br>';
		});		

	

	$app->get('/login', function() use ($app) {
		return $app['twig']->render('login.html');
	});

	$app->post('/login', function(Request $request) use ($app) {
		echo $_SESSION['REMOTE_ADDR']."<br>";
		$login = $request->get('login');
		echo "login $login <br>";
		$password = $request->get('password');
		echo "password $password <br>";
		
		$sql = "SELECT iduser uid FROM user WHERE user_login = ? AND user_pass = md5( ? )";
		echo "sql $sql <br>";
		$user = $app['db']->fetchAssoc($sql, array(
				(string) $login,
				(string) $password
			));
		echo json_encode($user)."<br>";
		

		$uid = $user['uid'];
		if( $uid == '' ){
			$uid = 0;	
		}

		$_SESSION['uid'] = $uid;
		echo $_SESSION['uid'];
		//return $app->redirect('/medialib/');
	});

	


	return $app;
?>